﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using RevitProject.SpaceManager.Models;
using System.Text;
using System.Threading.Tasks;

namespace RevitProject.SpaceManager.ViewModel.DialogViewModelPart
{
    public partial class ColumnSelectionDialogViewModel : ObservableObject
    {
        public class ColumnInfo : ObservableObject
        {
            private bool _isSelected;
            public string PropertyName { get; set; }
            public string Header { get; set; }
            public bool IsSelected { get => _isSelected; set => SetProperty(ref _isSelected, value); }
        }

        public ObservableCollection<ColumnInfo> AllColumns { get; } = new ObservableCollection<ColumnInfo>();

        public ColumnSelectionDialogViewModel(Dictionary<string, string> allAvailableColumns, List<string> initiallyVisibleProperties)
        {
            foreach (var kvp in allAvailableColumns.OrderBy(kv => kv.Value)) // 按显示名排序
            {
                AllColumns.Add(new ColumnInfo
                {
                    PropertyName = kvp.Key,
                    Header = kvp.Value,
                    IsSelected = initiallyVisibleProperties.Contains(kvp.Key) // 设置初始选中状态
                });
            }
        }

        /// <summary>
        /// 获取用户选择的可见列的属性名列表。
        /// </summary>
        public List<string> GetSelectedProperties()
        {
            return AllColumns.Where(c => c.IsSelected).Select(c => c.PropertyName).ToList();
        }
    }
}
